package com.specialized.orders.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.specialized.common.core.domain.Result;
import com.specialized.common.core.utils.DateUtils;
import com.specialized.common.security.utils.SecurityUtils;
import com.specialized.orders.common.domain.PickAirportVo.PickAirportVo;
import com.specialized.orders.utils.ChargRule;
import com.specialized.system.domain.SysUser;
import com.specialized.system.model.LoginUser;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.specialized.orders.mapper.PickAirportMapper;
import com.specialized.orders.common.domain.PickAirport;
import com.specialized.orders.service.IPickAirportService;

/**
 * 接机订单Service业务层处理
 *
 * @author djh
 * @date 2024-07-24
 */
@Log4j2
@Service
public class PickAirportServiceImpl implements IPickAirportService {
    @Autowired
    private PickAirportMapper pickAirportMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 查询接机订单
     *
     * @param pickairportId 接机订单主键
     * @return 接机订单
     */
    @Override
    public PickAirport selectPickAirportByPickairportId(Long pickairportId) {
        return pickAirportMapper.selectPickAirportByPickairportId(pickairportId);
    }

    /**
     * 查询接机订单列表
     *
     * @param pickAirport 接机订单
     * @return 接机订单
     */
    @Override
    public List<PickAirport> selectPickAirportList(PickAirport pickAirport) {
        Long userId = SecurityUtils.getUserId();
        pickAirport.setUserId(userId);
        return pickAirportMapper.selectPickAirportList(pickAirport);
    }

    /**
     * 新增接机订单
     *
     * @param pickAirport 接机订单
     * @return 结果
     */
    @Override
    public Result insertPickAirport(PickAirport pickAirport) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        List<PickAirport> byStatus = pickAirportMapper.findByStatus(sysUser.getUserId());
        if (byStatus.size()!=0){
            return Result.error("你有一笔未完成订单订单,请先支付");
        }

        //用户信息的封装

        String phonenumber = sysUser.getPhonenumber();
        pickAirport.setUserId(loginUser.getUserid());
        pickAirport.setUserTel(phonenumber);

        //订单号的生成
        String numb = RandomUtil.randomNumbers(6);
        String s = DateUtils.dateTime();
        String fill = StrUtil.fill(loginUser.getUserid().toString(), '0', 8, true);
        String orderCard = numb+s+fill;
        log.info(orderCard);
        pickAirport.setOrderCard(orderCard);
        pickAirport.setOrderStatus(1L);
        pickAirport.setCreateTime(DateUtils.getNowDate());

        ChargRule chargRule = new ChargRule();
        Double money = chargRule.computeMoneyrule(pickAirport);
        pickAirport.setRealPrice(BigDecimal.valueOf(money));

        //生成订单
        int i = pickAirportMapper.insertPickAirport(pickAirport);
        if (i>0){
            //发送订单到派单引擎
            rabbitTemplate.convertAndSend("insertPickAir", JSONObject.toJSONString(pickAirport), message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                return message;
            });
        }
        return Result.success("下单成功,待司机接单");
    }

    /**
     * 修改接机订单
     *
     * @param pickAirport 接机订单
     * @return 结果
     */
    @Override
    public int updatePickAirport(PickAirport pickAirport) {
        return pickAirportMapper.updatePickAirport(pickAirport);
    }

    /**
     * 批量删除接机订单
     *
     * @param pickairportIds 需要删除的接机订单主键
     * @return 结果
     */
    @Override
    public int deletePickAirportByPickairportIds(Long[] pickairportIds) {
        return pickAirportMapper.deletePickAirportByPickairportIds(pickairportIds);
    }

    /**
     * 删除接机订单信息
     *
     * @param pickairportId 接机订单主键
     * @return 结果
     */
    @Override
    public int deletePickAirportByPickairportId(Long pickairportId) {
        return pickAirportMapper.deletePickAirportByPickairportId(pickairportId);
    }


    @Override
    public List<PickAirportVo> findListAll(PickAirport pickAirport) {
        Long userId = SecurityUtils.getUserId();
        pickAirport.setDriverId(userId);
        return pickAirportMapper.findAll(pickAirport);
    }

    @Override
    public Result updateByOrderCard(String ordercard) {
        Integer i = pickAirportMapper.updateByOrderCard(ordercard);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }
}
